#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<cctype>
#include<string.h>
using namespace std;
class String {
	//符号重载(通过友元函数)
	friend ostream& operator<<(ostream& os, String& str);//重载流插入运算符
	friend istream& operator>>(istream& is, String& str);//重载流提取运算符
	friend String operator+(String& s1, String& s2);//s1+s2;
	friend String operator+(String& s1, char* p);//s1+char* p
	friend String operator+(char* p, String& s1);
	friend bool operator==(const String& s1, const String& s2);
	friend bool operator<(const String& s1, const String& s2);
	friend bool operator>(const String& s1, const String& s2);
	friend bool operator!=(const String& s1, const String& s2);
private:
	char* ptr;
	int len;
public:
	String();
	~String();//析构函数
	int find(char ch);
	String& operator=(const char* p);
	String& operator=(const String& another);
	char& operator[](int i);
	int size();
};
String::String() {
	ptr = new char[1];
	ptr[0] = 0;
	len = 0;
}
String::~String() {
	delete[]ptr;
}
int String::find(char ch) {
	for (int i = 0; i < strlen(this->ptr); i++) {
		if (this->ptr[i] == ch) {
			return i;
		}
	}
	return -1;
}
String& String::operator=(const char* p) {
	delete[]ptr;
	ptr = new char[strlen(p) + 1];
	len = strlen(p);
	strcpy(ptr, p);
	return *this;
}
String& String::operator=(const String& another) {
	if (this == &another) {
		return *this;
	}
	else {
		if (this->ptr != NULL) {
			delete[]this->ptr;
			this->ptr = NULL;
			this->len = 0;
		}//深拷贝
		this->ptr = new char[strlen(another.ptr) + 1];
		strcpy(this->ptr, another.ptr);
		return *this;
	}
}
char& String::operator[](int i) {
	if (i >= len) {
		cout << "WARNING:the arry is overflow !!!!!!" << endl;
	}
	else return ptr[i];//注意返回值为：一个对于char变量的引用！！！
}
int String::size() {
	return strlen(this->ptr) - 1;//获取字符串大小.
}
istream& operator>>(istream& is, String& str) {
	if (str.ptr != NULL) {
		delete[]str.ptr;
		str.ptr = NULL;
		str.len = 0;
	}
	char s[1001] = { '0' };
	cin >> s;
	str.len = strlen(s);
	str.ptr = new char[str.len + 1];//动态内存分配
	strcpy(str.ptr, s);
	return is;
}
ostream& operator<<(ostream& os, String& str) {
	os << str.ptr;
	return os;
}
String operator+(String& s1, char* p) {
	String str;
	char* ppd = new char[strlen(s1.ptr) + strlen(p) + 1];
	strcpy(ppd, s1.ptr);
	strcat(ppd, p);
	str.len = strlen(s1.ptr) + strlen(p);
	strcpy(str.ptr, ppd);
	return str;
}
String operator+(String& s1, String& s2) {
	String str;
	str.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
	str.len = strlen(s1.ptr) + strlen(s2.ptr);
	strcpy(str.ptr, s1.ptr);
	strcat(str.ptr, s2.ptr);
	return str;
}
String operator+(char* p, String& s1) {
	String str;
	char* ppd = new char[strlen(s1.ptr) + strlen(p) + 1];
	strcpy(ppd, s1.ptr);
	strcat(ppd, p);
	str.len = strlen(s1.ptr) + strlen(p);
	strcpy(str.ptr, ppd);
	return str;
}
bool operator==(const String& s1, const String& s2) {
	if (strlen(s1.ptr) != strlen(s2.ptr)) return false;
	else {
		for (int i = 0; i < strlen(s1.ptr); i++) {
			if (s1.ptr[i] != s2.ptr[i]) return false;
		}
		return true;
	}
}
bool operator!=(const String& s1, const String& s2) {
	if (strlen(s1.ptr) != strlen(s2.ptr)) return true;
	else {
		for (int i = 0; i < strlen(s1.ptr); i++) {
			if (s1.ptr[i] == s2.ptr[i]) return false;
		}
		return true;
	}
}
int main()
{
	String s1, s2, s3;
	s1 = "hello";
	s2 = "world";
	s3 = s1 + s2;
	cout << s3<<endl; 
	system("pause");
	exit(0);
    
}